<?php
include_once("../module/micro_shop/includes/plugin_coupons_class.php");
$couponsObj = new Coupons();

$error = "";
if($_POST['act']){//保存
    if($_POST['act']=='save'){//新增
        if(!empty($_POST['new_cpns_then'])){
            $coupons = array();
            foreach ($_POST['new_cpns_then'] as $k=>$val)
            {
                if(!($val>0) || !($_POST['new_cpns_num'][$k]>0)){
                    continue;
                }
                $coupons[] = array(
                    'cpns_if'=>$_POST['new_cpns_if'][$k],
                    'cpns_then'=>$val,
                    'cpns_num'=>$_POST['new_cpns_num'][$k],
                    'overtime'=>$_POST['new_overtime'][$k],
                );
            }
            
            if(!empty($coupons)){
                $data = array(
                    'name'          =>$_POST['name'],
                    'config_json'   =>json_encode($coupons),
                    'status'        =>$_POST['status'],
                    'create_user'   =>$_SESSION['ADMIN_USER']
                );
                $res = $couponsObj->add_edit_package_config($data);
                if($res){
                    msg("?m=micro_shop&s=coupons_package_config_list.php");
                }else{
                    $error = "添加失败，配置数据为空";
                }
            }else{
                $error = "优惠券配置无效";
            }
        }else{
            $error = "未添加优惠券配置";
        }
    }
    if($_POST['act']=='edit'){//编辑
        if(!empty($_POST['new_cpns_then']) || !empty($_POST['cpns_then'])){
            $coupons = array();
            if(!empty($_POST['cpns_then'])){
                foreach ($_POST['cpns_then'] as $kk=>$vv)
                {
                    if(!($vv>0) || !($_POST['cpns_num'][$kk]>0)){
                        continue;
                    }
                    $coupons[] = array(
                        'cpns_if'=>$_POST['cpns_if'][$kk],
                        'cpns_then'=>$vv,
                        'cpns_num'=>$_POST['cpns_num'][$kk],
                        'overtime'=>$_POST['overtime'][$kk],
                    );
                }
            }
            
            if(!empty($_POST['new_cpns_then'])){
                foreach ($_POST['new_cpns_then'] as $k=>$val)
                {
                    if(!($val>0) || !($_POST['new_cpns_num'][$k]>0)){
                        continue;
                    }
                    $coupons[] = array(
                        'cpns_if'=>$_POST['new_cpns_if'][$k],
                        'cpns_then'=>$val,
                        'cpns_num'=>$_POST['new_cpns_num'][$k],
                        'overtime'=>$_POST['new_overtime'][$k],
                    );
                }
            }

            if(!empty($coupons)){
                $data = array(
                    'id'            =>$_POST['id'],
                    'name'          =>$_POST['name'],
                    'config_json'   =>json_encode($coupons),
                    'status'        =>$_POST['status'],
                    'create_user'   =>$_SESSION['ADMIN_USER']
                );
                $res = $couponsObj->add_edit_package_config($data);
                if($res){
                    msg("?m=micro_shop&s=coupons_package_config_list.php");
                }else{
                    $error = "保存失败，配置数据为空";
                }
            }else{
                $error = "优惠券配置无效";
            }
        }else{
            $error = "未添加优惠券配置";
        }
    }
}

$de = array();
if($_GET['act']=='edit' && $_GET['id']>0){
    $sql = "select * from ".MICROSHOPCOUPONSCONFIG." where id={$_GET['id']}";
    $db->query($sql);
    $de = $db->fetchRow();
    if(!empty($de)){
        $de['coupons'] = json_decode($de['config_json'],true);
    }
}


$tpl->assign('error',$error);
$tpl->assign('de',$de);
$tpl->display('coupons_package_config_manage.htm');